
<div class="row animate fadeIn">
    <form id="upd_mm_read_form" class="form-horizontal"  method="post">
      <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit Meter Reading</i></h5>
        <div class="ibox-tools">
            <a id = "update_read" class="btn btn-sm btn-primary save-btn-tab">Save</a>    
            <a id = "cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
      </div>
        <div class="ibox-content" id="scan">
          <?php foreach($read_data as $rd){?>
            <input type="hidden" value="<?php echo $rd->mmeter_read_id; ?>" id="rd_hidden_id" />
            <div class="form-group"><label class="col-lg-3 control-label">Mother Meter</label> 
                    <div class="col-lg-5">
                        <select style="" id="mn" class="chosen-select" name="mm_no">
                          <?php foreach ($mmeters as $mm) { ?>
                          <option value="<?php echo $mm->mmeter_id; ?>" <?php echo ($rd->mmeter_id == $mm->mmeter_id ? 'selected' : ''); ?> ><?php echo $mm->mother_name; ?></option>
                          <?php } ?>
                        </select>
                    </div>
                    <!-- <div class="col-lg-1 input-s">
                        <a id = "getPast_btn" class="btn btn-sm btn-outline btn-info">Proceed</a>
                    </div> -->
            </div>
            
              
        </div>


        <div class="ibox-content" id="prevdata">
            
            <div class="form-inline form-group" >
              <div>
                  <label class="col-lg-3 control-label">Consumed :</label>
                <div class="col-lg-4">
                  <label id="consumed_u" class="control-label"><?php echo $rd->consumed; ?></label>
                </div>
              </div>
              <div>
                  <label class="col-lg-3 control-label">Reading Date :</label>
                <div class="col-lg-2">
                  <label id="" class="control-label"><?php echo $rd->read_date; ?></label>
                </div>
              </div>
            </div>
            <div class="form-inline form-group" >
              <div>
                  <label class="col-lg-3 control-label">Previous Meter Reading :</label>
                <div class="col-lg-2">
                  <label id="pmr_u" class="control-label"><?php echo $rd->previous_read; ?></label>
                </div>
              </div>
            </div>
            <div class="form-inline form-group" >
              <div>
                  <label class="col-lg-3 control-label">Present Meter Reading :</label>
                <div class="col-lg-2">
                  <label id="cmr_u" class="control-label"></label>
                </div>
              </div>
            </div>
        </div>


        <div class="ibox-content" id="calculate">
            <div>
                <div class="form-group"><label class="col-lg-3 control-label">New Meter Reading</label> 
                    <div class="col-lg-11 input-s">
                        <input type="text" name="" class="form-control" id="nmr_u">
                    </div>
                    <div class="col-lg-9 input-s">
                        <a id = "calc_read_upd" class="btn btn-sm btn-outline btn-info">Process</a>
                    </div>
                </div>
            </div>  
        </div>
        <!-- <div class="ibox-content" id="details">
            <div>
              <div class="col-lg-7 pull-right">
                    
              </div>
            </div>  
        </div> -->


      <?php } ?>

            
    </form>
</div>
            
<script>
  $(document).ready(function(){
    $('#upd_mm_read_form').data('serialize',$('#upd_mm_read_form').serialize());
    $(".chosen-select").chosen({width:"100%;"});
    $('#mn').prop('disabled', true).trigger("chosen:updated");


    var meter_id =0;
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();

 

    $('#calc_read_upd').click(function(){


        var nmr = $('#nmr_u').val();
        var consumed = nmr - parseInt($('#pmr_u').html());

              

        // $('#period_from').html($('#lrd').html());
        // $('#period_to').html("<?php echo date('d-M-y'); ?>");

        // $('#prev_read').html($('#pmr').html());
        $('#cmr_u').html(nmr);
        $('#consumed_u').html(consumed);

        // $('#details').show();
    });


    $('#update_read').click(function(){

      //mmeter_id = d[0].mmeter_id;
      // mm 
      // yyyy
      //$('#pmr').html();
      //$('#pres_read').html();
      //$('#consumed').html();
      //$('#period_to').html();
      $.ajax({
          type  : "POST",
          url   :"<?php echo base_url($this->session->userdata('forajax') . '/update_read'); ?>",
          dataType  : 'json',
          data : {"read_id":$('#rd_hidden_id').val() ,"cr":$('#cmr_u').html() ,"cons":$('#consumed_u').html() } ,
          success : function(d){                                                                                                                                                                                                        
           if(d[0].error == 1)
           {
            notifymsg("danger",d[0].msg);
           }else{
            $('#upd_mm_read_form').data('serialize',$('#upd_mm_read_form').serialize());
            notifymsg("success",'Reading Updated Successfully.');
              // $('#cur_calculate').attr('disabled',true);
              // $('#getPast_btn').attr('disabled',true);
              // $('input[name=new_mtr_read]').attr('readonly',true);
              // $('#submit_bill').attr('disabled',true);
              // $('#cancel_add').html('Complete');
           }
          },
          beforeSend: function(){
                      // $('#calc_read').attr('disabled',true);
                      // $('#getPast_btn').attr('disabled',true);
                      // $('#submit_read').attr('disabled',true);
          }
        });
    });



    function alert_cancel(){
            if($('#upd_mm_read_form').serialize()!=$('#upd_mm_read_form').data('serialize')){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_edit(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_edit(); active_search();
                notifymsg("hide","hide");
            }
        } 
        $('#cancel_add').click(function () {          
            alert_cancel();
          
        });


      $('#editX').click(function(){
            alert_cancel();      
       });


  });
       
</script>
